package raceReservationServer.saxParser;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;

import java.io.File;

public class XSLTExample {

    public static void main (String args []) {
        if (args.length != 2) {
            System.err.println ("Usage: java ejemploXML.EjemploXSLT stylesheet xmlfile");
            System.exit (1);
        }

        try {        
        	
        	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
            File datafile = new File(args[0]);
        	File stylesheet = new File(args[1]);            

            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(datafile);
            TransformerFactory tFactory = TransformerFactory.newInstance();
            StreamSource stylesource = new StreamSource(stylesheet);
            Transformer transformer = tFactory.newTransformer(stylesource);
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(new File( 
            		args[0].substring(0, args[0].lastIndexOf(".")) + ".html"));
            transformer.transform(source, result);
            
            System.out.println(" * Contenido HTML generado satisfactoriamente  ");
        } catch (Exception e) {
           System.out.println("# Error -> Main(): " + e.getMessage());
           e.printStackTrace();
        }
    }
}